package com.mallcloud.mall.mall.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.mall.api.entity.MallProductCategoryModel;
import com.mallcloud.mall.mall.service.MallProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品分类管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/mall/productCategory")
public class MallProductCategoryController {
    private final MallProductCategoryService mallCategoryService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('mall_productcategory_view')")
    public R<IPage<MallProductCategoryModel>> page(Page page, MallProductCategoryModel mallCategory) {
        return R.ok(mallCategoryService.page(page, Wrappers.query(mallCategory)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('mall_productcategory_view')")
    public R<List<MallProductCategoryModel>> list(MallProductCategoryModel mallCategory) {
        return R.ok(mallCategoryService.list(Wrappers.query(mallCategory)));
    }

	@ApiOperation("返回树形菜单集合")
	@GetMapping("/tree")
    @PreAuthorize("@pms.hasPermission('mall_productcategory_view')")
	public R<List<Tree<Long>>> getTree(Long parentId, String categoryName) {
		return R.ok(mallCategoryService.selectTree(parentId, categoryName));
	}

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('mall_productcategory_view')")
    public R<MallProductCategoryModel> getInfo(MallProductCategoryModel mallCategory) {
        return R.ok(mallCategoryService.getById(mallCategory.getCategoryId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('mall_productcategory_add')")
    public R<Boolean> save(@RequestBody MallProductCategoryModel dto) {
        return R.ok(mallCategoryService.saveCategory(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('mall_productcategory_edit')")
    public R<Boolean> updateById(@RequestBody MallProductCategoryModel dto) {
        return R.ok(mallCategoryService.updateCategoryById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{categoryId}")
    @PreAuthorize("@pms.hasPermission('mall_productcategory_del')")
    public R<Boolean> removeById(@PathVariable Long categoryId) {
        return R.ok(mallCategoryService.removeCategoryById(categoryId));
    }
}
